package com.atguigu.vodservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.vodservice.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 视频点播service
 */
@RestController
@Api(description = "视频点播管理")
@RequestMapping("/vod/video")
//@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 上传视频到阿里云 返回videoId
     *  获取视频播放地址urlBase
     *
     * @param file
     * @return
     */
    @ApiOperation("上传视频到阿里云")
    @PostMapping("uploadVideo")
    public R uploadVideo(MultipartFile file) {
        // 返回视频id
        String videoId = vodService.uploadVideo(file);
        // 获取阿里云视频播放地址
        String urlBase = vodService.getPlayInfo(videoId);
        return R.ok().data("videoId", videoId).data("urlBase",urlBase);
    }

    /**
     * 根据视频id删除阿里云视频
     * @param videoId
     * @return
     */
    @ApiOperation("根据视频id删除阿里云视频")
    @DeleteMapping("deleteVideo/{videoId}")
    public R deleteVideo(@ApiParam(value = "视频id",name = "videoId",required = true)@PathVariable String videoId){
        vodService.deleteVideo(videoId);
        return R.ok();
    }

    /**
     * 根据视频id集合批量删除视频
     * @param videoIdList
     * @return
     */
    @ApiOperation("根据视频id集合批量删除视频")
    @DeleteMapping("delete-batch")
    public R deleteVideoList(@RequestParam("videoIdList")List<String> videoIdList){
        vodService.removeVideList(videoIdList);
        return R.ok();
    }

    /**
     * 播放视频方式2 凭证播放
     * 根据视频id获取阿里云播放凭证
     * @param videoId
     * @return
     */
    @ApiOperation("根据视频id获取阿里云播放凭证")
    @GetMapping("playAuth/{videoId}")
    public R getPlayAuth(@ApiParam(value = "视频id",name = "videoId",required = true)@PathVariable String videoId){
        //视频播放凭证
        String playAuth = vodService.getPlayAuth(videoId);
        if (null == playAuth){
            return R.error();
        }else{
            return R.ok().data("playAuth",playAuth);
        }
    }
}
